package managedbean.IT;

import javax.faces.event.ActionEvent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.ejb.EJB;
import session.stateless.AccessControlSessionBeanLocal;
import javax.faces.bean.ManagedProperty;
import managedbean.UserSessionManagerBean;
import java.util.ArrayList;
import entity.AccessControl;
import java.util.HashSet;
import java.util.List;
import javax.faces.context.FacesContext;
import session.stateless.LogSessionBeanLocal;

@ManagedBean(name = "controlManagerBean")
@RequestScoped
public class ControlManagerBean {

    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private AccessControlSessionBeanLocal accessControlSessionBeanLocal;
    @EJB
    private LogSessionBeanLocal logSessionBeanLocal;
    private Long controlId;
    private String title;
    private String pageAddress;
    private String description;
    private String department;
    private String header;
    private String relatedPage;
    private String loadUpdateControl;
    private String loadAddControl;
    private String loadSearchControl;
    private String selectedControl;
    private static AccessControl control;
    private ArrayList<AccessControl> controls;
    private ArrayList<String> headers;

    //Page load
    public String getLoadUpdateControl() {

        try {

            control = (AccessControl) userSessionManagerBean.getSelectedItem();
            controlId = control.getControlId();
            title = control.getTitle();
            pageAddress = control.getPageAddress();
            description = control.getDescription();
            header = control.getHeader();
            department = control.getDepartment();
            relatedPage = control.getRelatedpage();

        } catch (Exception exception) {
            try {


                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/Web_IT/searchControl.xhtml");
            } catch (Exception ex) {
            }
        }

        return null;

    }

    public String getLoadAddControl() {
        return null;
    }

    public String getLoadSearchControl() {

        controls = accessControlSessionBeanLocal.getAccessControls();
        return null;
    }

// <editor-fold defaultstate="collapsed" desc="Getter & Setter methods. Click on the + sign on the left to edit the code.">
    public void setLoadAddControl(String loadAddControl) {
        this.loadAddControl = loadAddControl;
    }

    public void setLoadUpdateControl(String loadUpdateControl) {
        this.loadUpdateControl = loadUpdateControl;
    }

    public void setLoadSearchControl(String loadSearchControl) {
        this.loadSearchControl = loadSearchControl;
    }

    /**
     * @return the control
     */
    public AccessControl getControl() {
        return control;
    }

    /**
     * @param control the control to set
     */
    public void setControl(AccessControl control) {
        this.control = control;
    }

    /**
     * @return the controls
     */
    public ArrayList<AccessControl> getControls() {
        return controls;
    }

    /**
     * @param controls the controls to set
     */
    public void setControls(ArrayList<AccessControl> controls) {
        this.controls = controls;
    }

    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the accessControlSessionLocal
     */
    public AccessControlSessionBeanLocal getAccessControlSessionBeanLocal() {
        return accessControlSessionBeanLocal;
    }

    /**
     * @param accessControlSessionLocal the accessControlSessionLocal to set
     */
    public void setAccessControlSessionBeanLocal(AccessControlSessionBeanLocal accessControlSessionBeanLocal) {
        this.accessControlSessionBeanLocal = accessControlSessionBeanLocal;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the pageAddress
     */
    public String getPageAddress() {
        return pageAddress;
    }

    /**
     * @param pageAddress the pageAddress to set
     */
    public void setPageAddress(String pageAddress) {
        this.pageAddress = pageAddress;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the selectedControl
     */
    public String getSelectedControl() {
        return selectedControl;
    }

    /**
     * @param selectedControl the selectedControl to set
     */
    public void setSelectedControl(String selectedControl) {
        this.selectedControl = selectedControl;
    }

    /**
     * @return the department
     */
    public String getDepartment() {
        return department;
    }

    /**
     * @param deparment the department to set
     */
    public void setDepartment(String department) {
        this.department = department;
    }

    /**
     * @return the header
     */
    public String getHeader() {
        return header;
    }

    /**
     * @param header the header to set
     */
    public void setHeader(String header) {
        this.header = header;
    }

    /**
     * @return the controlId
     */
    public Long getControlId() {
        return controlId;
    }

    /**
     * @param controlId the controlId to set
     */
    public void setControlId(Long controlId) {
        this.controlId = controlId;
    }

    /**
     * @return the headers
     */
    public ArrayList<String> getHeaders() {
        return headers;
    }

    /**
     * @param headers the headers to set
     */
    public void setHeaders(ArrayList<String> headers) {
        this.headers = headers;
    }

    /**
     * @return the relatedPage
     */
    public String getRelatedPage() {
        return relatedPage;
    }

    /**
     * @param relatedPage the relatedPage to set
     */
    public void setRelatedPage(String relatedPage) {
        this.relatedPage = relatedPage;
    }

//</editor-fold>
    //Page method
    public void addNewControl(ActionEvent event) {

        try {
            accessControlSessionBeanLocal.addControl(title, pageAddress, description, department, header, relatedPage);
            logSessionBeanLocal.addLog("Add access control", "Access control created: " + title, "addControl.xhtml", userSessionManagerBean.getUserId());

            ArrayList<String> al = new ArrayList<String>();
            al.add("Control added");
            al.add("You have successfully created a control. Please go to Assign control page to assign the control to the role.");
            al.add("Click on the link to return to ");
            al.add("continue");
            al.add("Web_IT/addControl.xhtml");
            userSessionManagerBean.setSelectedItem(al);


            FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                    + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort()
                    + "/MerlionERP-war/success.xhtml");
        } catch (Exception ex) {
        }





    }

    public void doneUpdate(ActionEvent event) {
        try {
            AccessControl temp = accessControlSessionBeanLocal.getControl(title);
            if (temp == null) {//check if email taken
                accessControlSessionBeanLocal.updateControl(control.getControlId(), title, pageAddress, description, department, header, relatedPage);
            } else {
                if (temp.getControlId() == control.getControlId()) {//check if is modifying same record
                    accessControlSessionBeanLocal.updateControl(control.getControlId(), title, pageAddress, description, department, header, relatedPage);
                }
            }

            logSessionBeanLocal.addLog("Update access control", "Access control updated: " + title, "updateControl.xhtml", userSessionManagerBean.getUserId());

            ArrayList<String> al = new ArrayList<String>();
            al.add("Access control updated");
            al.add("You have successfully updated the access control.");
            al.add("Click on the link to return to ");
            al.add("continue");
            al.add("Web_IT/searchControl.xhtml");
            userSessionManagerBean.setSelectedItem(al);

            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() + "/MerlionERP-war/success.xhtml");

        } catch (Exception ex) {
        }



    }

    public void removeControl(ActionEvent event) {
        try {
            accessControlSessionBeanLocal.deleteControl(control.getControlId());
            
            logSessionBeanLocal.addLog("Delete access control", "Access control deleted: " + title, "updateControl.xhtml", userSessionManagerBean.getUserId());

            ArrayList<String> al = new ArrayList<String>();
            al.add("Access control removed");
            al.add("You have successfully removed the access control. The user can no longer grant access given to this page.");
            al.add("Click on the link to return to ");
            al.add("continue");
            al.add("Web_IT/searchControl.xhtml");
            userSessionManagerBean.setSelectedItem(al);

            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() + "/MerlionERP-war/success.xhtml");
        } catch (Exception ex) {
        }

    }

    public void edit(ActionEvent event) {
        userSessionManagerBean.setSelectedItem((AccessControl) control);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() + "/MerlionERP-war/Web_IT/updateControl.xhtml");

        } catch (Exception ex) {
        }
    }

    public List<String> complete(String query) {

        setHeaders(new ArrayList<String>());

        for (AccessControl o : accessControlSessionBeanLocal.getAccessControls()) {
            getHeaders().add(o.getHeader());
        }

        String header = null;
        List<String> matched = new ArrayList<String>();
        for (int i = 0; i < getHeaders().size(); i++) {
            header = getHeaders().get(i);
            if (header.startsWith(query)) {
                matched.add(header);
            }
        }
        HashSet hashSet = new HashSet(matched);
        ArrayList<String> temp = new ArrayList(hashSet);
        return temp;

    }
}
